package org.ns.summer.classpath.scan.parse;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

import org.ns.summer.classpath.scan.ScanFile;
import org.ns.summer.classpath.scan.ScanFileUtils;
import org.ns.summer.classpath.scan.SuffixScanFileParse;
/** 基本jar解析器, 只解析目录下的.jar文件得到所有文件内容, 不对jar压缩包内的.jar 或.zip,非class文件均做解压解析.只得到对应的文件. <BR/>
 * 
 * @author 张星宇
 *
 */
public class JarSuffixScanFileParse implements SuffixScanFileParse{

	@Override
	public List<ScanFile> parseScanFiles(String scanRootPath, String filePath, String suffix) {
		List<ScanFile> scanFiles = new ArrayList<ScanFile>();
		try (FileInputStream in = new FileInputStream(ScanFileUtils.getFile(filePath))){
			List<JarEntry> jarEntries = getJarEntries(in);
			for (JarEntry jarEntry : jarEntries) {
				String jarEntryPath = jarEntry.getName();
				if(!jarEntry.isDirectory()){
					ScanFile scanFile = new ScanFile(filePath, jarEntryPath, filePath+"!/"+jarEntryPath);
					scanFiles.add(scanFile);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return scanFiles;
	}
	
	public List<JarEntry> getJarEntries(InputStream in){
		List<JarEntry> jarEntries = new ArrayList<JarEntry>();
		try (JarInputStream jin = new JarInputStream(in);) {
			JarEntry jarEntry = null;
			while((jarEntry = jin.getNextJarEntry())!=null){
				jarEntries.add(jarEntry);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jarEntries;
	}
	

}
